using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BlockBase : MonoBehaviour
{
    public float maxXZ = 50;
    public float maxY = 50;

    [HideInInspector]
    public Vector3 OrigPos;
    protected int layer = 0;
    protected int treeLayer = 0;

    protected virtual void Awake()
    {
        layer = LayerMask.GetMask("Block");
        treeLayer = LayerMask.GetMask("Tree");
        MessageManager.BindAction(MsgKey.ClickRight, RecordOriPos);
        MessageManager.BindAction(MsgKey.ClickForward, RecordOriPos);
        MessageManager.BindAction(MsgKey.ClickUp, RecordOriPos);

        MessageManager.BindAction(MsgKey.MoveToRight, MoveToRight);
        MessageManager.BindAction(MsgKey.MoveToForward, MoveToForward);
        MessageManager.BindAction(MsgKey.MoveToUp, MoveToTop);
        MessageManager.BindAction(MsgKey.MoveToOrig, MoveToOrig);
    }

    protected virtual void Start()
    {
        LevelManager.instance.blocks.Add(this);
    }

    protected virtual void OnDestroy()
    {
        MessageManager.Remove(MsgKey.ClickRight, RecordOriPos);
        MessageManager.Remove(MsgKey.ClickForward, RecordOriPos);
        MessageManager.Remove(MsgKey.ClickUp, RecordOriPos);

        MessageManager.Remove(MsgKey.MoveToRight, MoveToRight);
        MessageManager.Remove(MsgKey.MoveToForward, MoveToForward);
        MessageManager.Remove(MsgKey.MoveToUp, MoveToTop);
        MessageManager.Remove(MsgKey.MoveToOrig, MoveToOrig);
    }

    void RecordOriPos()
    {
        OrigPos = transform.position;
    }

    protected virtual void Update()
    { 
    }

    protected virtual void MoveToRight()
    {
        Ray ray = new Ray(transform.position, Vector3.right);
        if (Physics.Raycast(ray, 100, layer))
        {
            gameObject.SetActive(false);
        }
        else
        {
            gameObject.SetActive(true);
        }
        if (Physics.Raycast(ray, 100, treeLayer))
        {
            transform.position = new Vector3(maxXZ-3, OrigPos.y, OrigPos.z);
        }
        else
        {
            transform.position = new Vector3(maxXZ, OrigPos.y, OrigPos.z);
        }

    }

    protected virtual void MoveToForward()
    {
        Ray ray = new Ray(transform.position, Vector3.back);
        if (Physics.Raycast(ray, 100, layer))
        {
            gameObject.SetActive(false);
        }
        else
        {
            gameObject.SetActive(true);
        }
        if (Physics.Raycast(ray, 100, treeLayer))
        {
            transform.position = new Vector3(OrigPos.x, OrigPos.y, -maxXZ+3);
        }
        else
        {
            transform.position = new Vector3(OrigPos.x, OrigPos.y, -maxXZ);
        }
    }

    protected virtual void MoveToTop()
    {
        Ray ray = new Ray(transform.position, Vector3.up);
        if (Physics.Raycast(ray, 100, layer))
        {
            gameObject.SetActive(false);
        }
        else
        {
            gameObject.SetActive(true);
        }
        transform.position = new Vector3(OrigPos.x, maxY, OrigPos.z);
    }

    protected virtual void MoveToOrig()
    {
        gameObject.SetActive(true);
        transform.position = OrigPos;
    }
}
